package com.leecode.xiehf.page_02;

import com.leecode.Printer;

import java.util.ArrayList;
import java.util.List;

/**
 * 给你一个整数数组 nums ，数组中的元素 互不相同 。返回该数组所有可能的子集（幂集）。
 * <p>
 * 解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
 * <p>
 * https://leetcode-cn.com/problems/subsets/
 */
public class Solution_0078 extends Printer {

    public static void main(String[] args) {
        Solution_0078 solution = new Solution_0078();
        List<List<Integer>> s = solution.subsets(new int[]{1, 2, 3, 4});
        printIntList(s);
    }

    public List<List<Integer>> subsets(int[] nums) {
        int len = nums.length;
        List<List<Integer>> list = new ArrayList<>();
        list.add(new ArrayList<>());
        for (int i = 1; i < len + 1; i++) {
            list.addAll(gen(nums, i));
        }
        return list;
    }

    private List<List<Integer>> gen(int[] nums, int k) {
        List<List<Integer>> list = new ArrayList<>();
        List<Integer> subList = new ArrayList<>();
        dfs(list, subList, nums, 0, k);
        return list;
    }


    private void dfs(List<List<Integer>> list, List<Integer> subList, int[] nums, int num, int k) {
        if (k == 0) {
            list.add(new ArrayList<>(subList));
            return;
        }
        for (int i = num; i < nums.length; i++) {
            subList.add(nums[i]);
            dfs(list, subList, nums, i + 1, k - 1);
            subList.remove(subList.size() - 1);
        }
    }
}
